<div class="layui-fluid layui-anim zzdz-anim" id="zzdz-dict" lay-title="字典表管理">
    <div class="layui-row zzdz-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body zzdz-table-full">
                    <form class="layui-form layui-table-form" lay-filter="dict-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm"> 字典类型</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="type" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary zzdz-button-blue-plain table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary zzdz-button-green-plain table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                                     shiro:hasAnyPermissions="dict:add,dict:update,dict:export">
                                    <i class="layui-icon">&#xe875;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="dictTable" lay-data="{id: 'dictTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="dict-option">
    <span shiro:lacksPermission="dict:list,dict:update,dict:delete">
        <span class="layui-badge-dot zzdz-bg-orange"></span> 无权限
    </span>
    <a lay-event="detail" shiro:hasPermission="dict:list"><i
                class="layui-icon zzdz-edit-area zzdz-green">&#xe7a5;</i></a>
    <a lay-event="edit" shiro:hasPermission="dict:update"><i
                class="layui-icon zzdz-edit-area zzdz-blue">&#xe7a4;</i></a>
    <a lay-event="del" shiro:hasPermission="dict:delete"><i class="layui-icon zzdz-edit-area zzdz-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'laydate', 'form', 'table', 'zzdz'], function () {
        var $ = layui.jquery,
            laydate = layui.laydate,
            zzdz = layui.zzdz,
            form = layui.form,
            table = layui.table,
            dropdown = layui.dropdown,
            $view = $('#zzdz-dict'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            tableIns,
            createTimeFrom,
            createTimeTo;

        form.render();

        initTable();

        laydate.render({
            elem: '#dict-createTime',
            range: true,
            trigger: 'click'
        });

        dropdown.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                var checkStatus = table.checkStatus('dictTable');
                if (name === 'add') {
                    zzdz.modal.open('新增字典', 'dict/add', {
                        btn: ['提交', '重置'],
                        area: $(window).width() <= 750 ? '95%' : '660px',
                        yes: function (index, layero) {
                            $('#dict-add').find('#submit').trigger('click');
                        },
                        btn2: function () {
                            $('#dict-add').find('#reset').trigger('click');
                            return false;
                        }
                    });
                }
                if (name === 'delete') {
                    if (!checkStatus.data.length) {
                        zzdz.alert.warn('请选择需要删除的字典');
                    } else {
                        zzdz.modal.confirm('删除字典', '确定删除该字典？', function () {
                            var dictIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                dictIds.push(item.id)
                            });
                            deleteDicts(dictIds.join(','));
                        });
                    }
                }
                if (name === 'export') {
                    var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
                    params.pageSize = $view.find(".layui-laypage-limits option:selected").val();
                    params.pageNum = $view.find(".layui-laypage-em").next().html();
                    zzdz.download(ctx + 'dict/excel', params, '字典信息表.xlsx');
                }
            },
            options: [{
                name: 'add',
                title: '新增字典',
                perms: 'dict:add'
            }, {
                name: 'delete',
                title: '删除字典',
                perms: 'dict:delete'
            }, {
                name: 'export',
                title: '导出Excel',
                perms: 'dict:export'
            }]
        });

        table.on('tool(dictTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'detail') {
                zzdz.modal.view('字典信息', 'dict/view/' + data.id, {
                    area: $(window).width() <= 750 ? '95%' : '660px'
                });
            }
            if (layEvent === 'del') {
                zzdz.modal.confirm('删除字典', '确定删除该字典？', function () {
                    deleteDicts(data.id);
                });
            }
            if (layEvent === 'edit') {
                zzdz.modal.open('修改字典', 'dict/edit/' + data.id, {
                    area: $(window).width() <= 750 ? '90%' : '660px',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#dict-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
        });

        table.on('sort(dictTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            createTimeTo = null;
            createTimeFrom = null;
            tableIns.reload({where: getQueryParams(), page: {curr:1} , initSort: sortObject});
        });

        function getQueryParams() {
            var type = $searchForm.find('input[name="type"]').val();

            return {
                type: type,
                invalidate_ie_cache: new Date()
            };
        }

        function initTable() {
            tableIns = zzdz.table.init({
                elem: $view.find('table'),
                id: 'dictTable',
                url: ctx + 'dict/list',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'type', title: '字典类型', minWidth: 120},
                    {field: 'description', title: '描述', minWidth: 120},
                    {field: 'remarks', title: '备注', minWidth: 120},
                    {field: 'createTime', title: '创建时间', minWidth: 120},
                    {title: '操作', toolbar: '#dict-option', minWidth: 140}
                ]]
            });
        }

        /**
         * 删除字典表
         * @param ids
         */
        function deleteDicts(ids) {
            zzdz.get(ctx + 'dict/delete/' + ids, null, function () {
                zzdz.alert.success('删除字典成功');
                $query.click();
            });
        }
    })
</script>